//
//  ViewController.m
//  03-HomeWork
//
//  Created by dllo on 16/7/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "TextButton.h"
#import "ImageButton.h"
#import "UIView+Frame.h"
@interface ViewController () <UITextFieldDelegate>
@property (nonatomic, strong) UITextField *userTextField;
@property (nonatomic, strong) UITextField *passwordTextField;
@property (nonatomic, strong) UILabel *inputLabel;
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) TextButton *loginButton;
@property (nonatomic, strong) ImageButton *questionButton;
@property (nonatomic, strong) TextButton *forgotPasswordButton;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    通过UILabel封装实现只显示文字的Button
//    通过UIImageView封装实现只显示图片的Button 以上两种方式, 要考虑到通用性。
//    使用自己封装的控件, 加入到前两天编写的登陆页中
    //1>
    TextButton *textButton = [[TextButton alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    textButton.userInteractionEnabled = YES;
    textButton.backgroundColor = [UIColor yellowColor];
    textButton.text = @"点击";
    textButton.textAlignment = NSTextAlignmentCenter;
    textButton.textColor = [UIColor whiteColor];
    [textButton addTapActionTarget:self action:@selector(buttonClicked:)];
//    [self.view addSubview:textButton];
   //2>
    ImageButton *imageButton = [[ImageButton alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
    imageButton.image = [UIImage imageNamed:@"icon_username_select@2x.png"];
    [imageButton addTapActionTarget:self action:@selector(buttonClicked:)];
//    [self.view addSubview:imageButton];

    //3>
   
//backGround
    UIImageView *backGroundImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    backGroundImageView.image = [UIImage imageNamed:@"welcome_2.jpg"];
    [self.view addSubview:backGroundImageView];
//logo
    self.logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2, 70, 180, 220)];
    CGPoint point = self.logoImageView.center;
    point.x = self.view.frame.size.width / 2;
    self.logoImageView.center = point;
    self.logoImageView.image = [UIImage imageNamed:@"logo_lagou@2x.png"];
    [self.view addSubview:self.logoImageView];
//输入框
    self.inputLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 300, self.view.frame.size.width - 20, 110)];
    //    inputLabel.backgroundColor = [UIColor whiteColor];
    self.inputLabel.layer.cornerRadius = 4;
    self.inputLabel.clipsToBounds = YES;
//    inputLabel.layer.borderWidth = 4;
//    label和imageView都要开用户交互
    self.inputLabel.userInteractionEnabled = YES;
    [self.view addSubview:self.inputLabel];
// 用户登录
    UIImageView *userLoginImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    userLoginImageView.backgroundColor = [UIColor colorWithRed:0.15 green:0.13 blue:0.11 alpha:1.00];
    userLoginImageView.image = [UIImage imageNamed:@"icon_username@2x.png"];
    [self.inputLabel addSubview:userLoginImageView];
    self.userTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 0, 310, 50)];
    self.userTextField.backgroundColor = [UIColor grayColor];
    self.userTextField.alpha = 0.4;
    self.userTextField.textColor = [UIColor whiteColor];
    self.userTextField.placeholder = @"请输入已验证的手机号或邮箱";
    self.userTextField.textColor = [UIColor whiteColor];
    self.userTextField.delegate = self;
    [self.inputLabel addSubview:self.userTextField];
        
    UIImageView *passwordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, 50, 50)];
    passwordImageView.backgroundColor = [UIColor colorWithRed:0.15 green:0.13 blue:0.11 alpha:1.00];
    passwordImageView.image = [UIImage imageNamed:@"icon_password@2x.png"];
    [self.inputLabel addSubview:passwordImageView];
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 60, 310, 50)];
        
    self.passwordTextField.backgroundColor = [UIColor grayColor];
    self.passwordTextField.alpha = 0.4;
    self.passwordTextField.placeholder = @"请输入密码";
    self.passwordTextField.delegate = self;
    [self.inputLabel addSubview:self.passwordTextField];
// 忘记密码
    self.questionButton = [[ImageButton alloc] initWithFrame:CGRectMake(200, 420, 40, 40)];
    self.questionButton.image = [UIImage imageNamed:@"question.png"];
    [self.view addSubview:self.questionButton];
    self.forgotPasswordButton = [[TextButton alloc] initWithFrame:CGRectMake(240, 420, 150, 50)];
    self.forgotPasswordButton.text = @"忘记密码";
    self.forgotPasswordButton.font = [UIFont systemFontOfSize:20];
    self.forgotPasswordButton.textColor = [UIColor whiteColor];
    [self.forgotPasswordButton addTapActionTarget:self action:@selector(forgotPasswordButtonClicked)];
    [self.view addSubview:self.forgotPasswordButton];
        
// 登录
    self.loginButton = [[TextButton alloc] initWithFrame:CGRectMake(20, 460, self.view.frame.size.width - 40, 40)];
    self.loginButton.text = @"登录";
    self.loginButton.textAlignment = NSTextAlignmentCenter;
    self.loginButton.textColor = [UIColor whiteColor];
    self.loginButton.layer.cornerRadius = 4;
    self.loginButton.backgroundColor = [UIColor colorWithRed:0.00 green:0.68 blue:0.53 alpha:1.00];
    [self.loginButton addTapActionTarget:self action:@selector(loginButtonClicked)];
    [self.view addSubview:self.loginButton];
// 注册按钮
    TextButton *registerButton = [[TextButton alloc] initWithFrame:CGRectMake(20, 530, self.view.frame.size.width - 40, 40)];
    registerButton.frame = CGRectMake(20, 530, self.view.frame.size.width - 40, 40);
    registerButton.layer.cornerRadius = 4;
    registerButton.layer.borderWidth = 2;
    registerButton.layer.borderColor = [UIColor colorWithRed:0.00 green:0.68 blue:0.53 alpha:1.00].CGColor;
    registerButton.text = @"注册拉勾";
    registerButton.textColor = [UIColor colorWithRed:0.00 green:0.68 blue:0.53 alpha:1.00];
    registerButton.textAlignment = NSTextAlignmentCenter;
    [registerButton addTapActionTarget:self action:@selector(registerButtonClicked)];
    [self.view addSubview:registerButton];
}
  //当textField开始编辑时（动画效果）
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [UIView animateWithDuration:0.5 animations:^{
        self.logoImageView.alpha = 0;
        self.inputLabel.pf_y = 200;
        self.loginButton.pf_y = 360;
        self.questionButton.pf_y = 320;
        self.forgotPasswordButton.pf_y = 320;
        
    }];
}


- (void)forgotPasswordButtonClicked {
    
}
//点击注册收起键盘
- (void)registerButtonClicked {
   
    }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    // 收起键盘的方式
    // 点击return收起键盘
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField endEditing:YES];
    return YES;
    }
// 点击空白处收起键盘
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (![self.userTextField isExclusiveTouch] || ![self.passwordTextField isExclusiveTouch]) {
            [self.userTextField resignFirstResponder];
            [self.passwordTextField resignFirstResponder];
        [UIView animateWithDuration:0.5 animations:^{
            self.logoImageView.alpha = 1;
            self.inputLabel.pf_y = 300;
            self.loginButton.pf_y = 460;
            self.questionButton.pf_y = 420;
            self.forgotPasswordButton.pf_y = 420;
            
        }];    }
    }
// 点击登录按钮收起键盘
- (void)loginButtonClicked {
        [self.userTextField resignFirstResponder];
        [self.passwordTextField resignFirstResponder];
    }
- (void)buttonClicked:(UITapGestureRecognizer *)tap {
    NSLog(@"我被点了");
}

@end
